package com.night.xjh.controller;

import com.night.common.entity.ResultVo;
import com.night.common.entity.xjh.Header;
import com.night.xjh.service.HeaderService;
import com.night.xjh.vo.HeaderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: HeaderController
 * @Description: //TODO
 * @author: xjh
 * @date: 2022/10/25-9:51
 * @version: v1.0
 */
@RestController
@RequestMapping("/api/xjh/header")
public class HeaderController {
    @Autowired
    private HeaderService headerService;
    //-----------------------------------Header-------------------------------------
// add,delete,update,getAll,getById,getByCondition
    @RequestMapping("/add")
    public ResultVo<?> add(@RequestBody HeaderVo headerVo){
        headerService.add(dataFormat(headerVo));
        return ResultVo.SUCCESS;
    }
    @RequestMapping("/delete")
    public ResultVo<?> delete(@RequestBody HeaderVo headerVo){
        headerService.delete(headerVo.getId());
        return ResultVo.SUCCESS;
    }
    @RequestMapping("/update")
    public ResultVo<?> updateHeader(@RequestBody HeaderVo headerVo){
        headerService.update(dataFormat(headerVo));
        return ResultVo.SUCCESS;
    }
    @RequestMapping("/getAll")
    public ResultVo<?> getAllHeader(@RequestBody HeaderVo headerVo){
        return new ResultVo<>(headerService.getAll(headerVo.getPageNum(),headerVo.getPageSize()));
    }

    @RequestMapping("/getById")
    public ResultVo<?> getById(@RequestBody HeaderVo headerVo){
        return new ResultVo<>(headerService.getById(headerVo.getId()));
    }

    @RequestMapping("/getByCondition")
    public ResultVo<?> getByCondition(@RequestBody HeaderVo headerVo){
        return new ResultVo<>(
                headerService.getByCondition(
                        dataFormat(headerVo),
                        headerVo.getPageNum(),
                        headerVo.getPageSize()
                )
        );
    }


    private Header dataFormat(HeaderVo headerVo){
        Header header=new Header();
        header.setId(headerVo.getId());
        header.setName(headerVo.getName());
        header.setPhone(headerVo.getPhone());
        header.setEmail(headerVo.getEmail());
        return header;
    }
}
